package com.goal98.team.test.integration

import grails.test.*
import com.goal98.grails.plugin.account.domain.Account
import com.goal98.grails.plugin.account.domain.Credit
import com.goal98.grails.plugin.account.domain.Debit

class AccountServiceTests extends GroovyTestCase {

    def accountService

    protected void setUp() {
        super.setUp()
    }

    protected void tearDown() {
        super.tearDown()
    }



    void testCredit() {

        final def accountNumber = 'acc001'
        Account account = new Account(balance:0, reference:accountNumber)
        account.save(flush:true)

        final def amount = 100
        accountService.credit(account, amount, '')

        assertEquals(amount, account.balance)
        assertEquals(accountNumber, account.reference)

        Credit credit = account.transactions[0]
        assertEquals(amount, credit.amount)
        assertEquals(accountNumber, credit.account.reference)

    }

    void testDebit() {

        final def accountNumber = 'acc002'
        Account account = new Account(balance:1000, reference:accountNumber)
        account.save(flush:true)

        final def amount = 100
        accountService.debit(account, amount, '')

        assertEquals(1000 - amount, account.balance)
        assertEquals(accountNumber, account.reference)

        Debit debit = account.transactions[0]
        assertEquals(amount, debit.amount)
        assertEquals(accountNumber, debit.account.reference)

    }
}